import * as THREE from 'three';
const ThreeBSP = require('three-js-csg')(THREE)

/**
 * 门墙窗户类
 */
export default class Wall {
    constructor(_scene, _cargo) {
        console.log('门墙窗户类')

        this.parent = _scene;
      
        this.createWall(); // 生成墙
        // this.createDoor(_cargo); // 生成门
        // this.createWindow(_cargo); // 生成窗户
    }
    /////////////////////////////// 生成墙 /////////////////////////////////////
    createWall () {
        let wallMat = new THREE.MeshPhongMaterial({color: 0x1C86EE}); // 灰色
        let matArray = new Array(6).fill(wallMat)
        
        // 创建实体墙
        this.createCubeWall(4, 200, 400, 0, matArray, -298, 100, 0, "墙面");
        this.createCubeWall(4, 200, 400, 1, matArray, 298, 100, 0, "墙面");
        this.createCubeWall(4, 200, 600, 1.5, matArray, 0, 100, -198, "墙面");
        this.createCubeWall(4, 200, 600, 1.5, matArray, 0, 100, 198, "墙面");

        // // 创建带门洞的墙
        // let wall = this.createWallWithHole(2600, 200, 10, 0, matArray, 0, 100, 700, "墙面");
        // let objects_cube = [
        //     this.createWallWithHole(200, 180, 10, 0, matArray, -600, 90, 700, "前门1"),
        //     this.createWallWithHole(200, 180, 10, 0, matArray, 600, 90, 700, "前门2"),
        //     this.createWallWithHole(100, 100, 10, 0, matArray, -900, 90, 700, "窗户1"),
        //     this.createWallWithHole(100, 100, 10, 0, matArray, 900, 90, 700, "窗户2"),
        //     this.createWallWithHole(100, 100, 10, 0, matArray, -200, 90, 700, "窗户3"),
        //     this.createWallWithHole(100, 100, 10, 0, matArray, 200, 90, 700, "窗户4"),
        // ];
        // //墙上挖门，通过两个几何体生成BSP对象
        // this.createResultBsp(wall, objects_cube);
    };
    // 创建实体墙
    createCubeWall(width, height, depth, angle, material, x, y, z, name){
        let cubeGeometry = new THREE.BoxGeometry(width, height, depth);
        let cube = new THREE.Mesh(cubeGeometry, material);
        cube.position.x = x;
        cube.position.y = y;
        cube.position.z = z;
        cube.rotation.y += angle * Math.PI; //-逆时针旋转,+顺时针
        cube.name = name;
        this.parent.add(cube);
    }

    //墙上挖门，通过两个几何体生成BSP对象
    createResultBsp(bsp, objects_cube) {
        let material = new THREE.MeshPhongMaterial({
          color: 0x9cb2d1,
          specular: 0x9cb2d1,
          shininess: 30,
          transparent: true,
          opacity: 1
        });
        let BSP = new ThreeBSP(bsp);
        for (let i = 0; i < objects_cube.length; i++) {
          let less_bsp = new ThreeBSP(objects_cube[i]);
          BSP = BSP.subtract(less_bsp);
        }
        let result = BSP.toMesh(material);
        result.material.flatshading = THREE.FlatShading;
        result.geometry.computeFaceNormals(); //重新计算几何体侧面法向量
        result.geometry.computeVertexNormals();
        result.material.needsUpdate = true; //更新纹理
        result.geometry.buffersNeedUpdate = true;
        result.geometry.uvsNeedUpdate = true;
        this.parent.add(result);
      }

    // 创建带门洞的墙
    createWallWithHole(width, height, depth, angle, material, x, y, z, name){
        let cubeGeometry = new THREE.BoxGeometry(width, height, depth);
        let cube = new THREE.Mesh(cubeGeometry, material);
        cube.position.x = x;
        cube.position.y = y;
        cube.position.z = z;
        cube.rotation.y += angle * Math.PI;
        cube.name = name;
        return cube;
    }

    ///////////////////////////// 生成门 /////////////////////////////
    // 生成门
    createDoor(_cargo){
        this.createDoorLeft(_cargo,100, 180, 2, 0, -700, 90, 700, "左门1")
        this.createDoorRight(_cargo,100, 180, 2, 0, -500, 90, 700, "右门1")
        this.createDoorLeft(_cargo,100, 180, 2, 0, 500, 90, 700, "左门2")
        this.createDoorRight(_cargo,100, 180, 2, 0, 700, 90, 700, "右门2")
    }

    //创建门_左侧
    createDoorLeft(_cargo,width, height, depth, angle, x, y, z, name) {
        let texture = _cargo['warehouse/door_left'];
        let doorgeometry = new THREE.BoxGeometry(width, height, depth);
        doorgeometry.translate(50, 0, 0);
        let doormaterial = new THREE.MeshBasicMaterial({
        map: texture,
        color: 0xffffff
        });
        doormaterial.opacity = 1.0;
        doormaterial.transparent = true;
        let door = new THREE.Mesh(doorgeometry, doormaterial);
        door.position.set(x, y, z);
        door.rotation.y += angle * Math.PI; //-逆时针旋转,+顺时针
        door.name = name;
        this.parent.add(door);
    }

    //创建门_右侧
    createDoorRight(_cargo,width, height, depth, angle, x, y, z, name) {
        let texture = _cargo['warehouse/door_right'];
        let doorgeometry = new THREE.BoxGeometry(width, height, depth);
        doorgeometry.translate(-50, 0, 0);
        let doormaterial = new THREE.MeshBasicMaterial({
          map: texture,
          color: 0xffffff
        });
        doormaterial.opacity = 1.0;
        doormaterial.transparent = true;
        let door = new THREE.Mesh(doorgeometry, doormaterial);
        door.position.set(x, y, z);
        door.rotation.y += angle * Math.PI; //-逆时针旋转,+顺时针8
        door.name = name;
        this.parent.add(door);
    }

    ///////////////////////////// 生成窗户 /////////////////////////////

    createWindow(_cargo,width, height, depth, angle, x, y, z, name) {
        let texture = _cargo['warehouse/window'];
        let windowgeometry = new THREE.BoxGeometry(width, height, depth);
        let windowmaterial = new THREE.MeshBasicMaterial({
            map: texture,
            color: 0xffffff
        });
        windowmaterial.opacity = 1.0;
        windowmaterial.transparent = true;
        let window = new THREE.Mesh(windowgeometry, windowmaterial);
        window.position.set(x, y, z);
        window.rotation.y += angle * Math.PI; //-逆时针旋转,+顺时针
        window.name = name;
        this.parent.add(window);
    }
  
    ///////////////////////////// 公共方法 /////////////////////////////
   
    update (_props) {
       
    };
    // On Window Resize
    onWindowResize (vpW, vpH, pixelRatio) {
        
    };
}